package com.fqgame.user.controller;

import com.fqgame.user.entity.UserInfo;
import com.fqgame.user.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户控制器
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserInfoService userInfoService;

    /**
     * 获取用户信息
     */
    @GetMapping("/{id}")
    public ResponseEntity<UserInfo> getUserInfo(@PathVariable Long id) {
        log.info("获取用户信息，用户ID：{}", id);
        UserInfo userInfo = userInfoService.getById(id);
        if (userInfo == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(userInfo);
    }

    /**
     * 创建订单（分布式事务示例）
     */
    @PostMapping("/order")
    public ResponseEntity<Map<String, Object>> createOrder(
            @RequestParam Long userId,
            @RequestParam Long productId,
            @RequestParam String productName,
            @RequestParam BigDecimal amount) {
        log.info("创建订单请求，用户ID：{}，商品ID：{}，金额：{}", userId, productId, amount);
        
        try {
            Long orderId = userInfoService.createOrderWithTransaction(userId, productId, productName, amount);
            
            Map<String, Object> result = new HashMap<>();
            result.put("success", true);
            result.put("orderId", orderId);
            result.put("message", "订单创建成功");
            
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            log.error("创建订单失败", e);
            
            Map<String, Object> result = new HashMap<>();
            result.put("success", false);
            result.put("message", e.getMessage());
            
            return ResponseEntity.badRequest().body(result);
        }
    }

    /**
     * 手动更新用户积分（消息队列示例）
     */
    @PostMapping("/points")
    public ResponseEntity<Map<String, Object>> updatePoints(
            @RequestParam Long userId,
            @RequestParam Integer points,
            @RequestParam String reason) {
        log.info("手动更新用户积分，用户ID：{}，积分：{}，原因：{}", userId, points, reason);
        
        try {
            userInfoService.updateUserPoints(userId, points, reason);
            
            Map<String, Object> result = new HashMap<>();
            result.put("success", true);
            result.put("message", "积分更新成功");
            
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            log.error("积分更新失败", e);
            
            Map<String, Object> result = new HashMap<>();
            result.put("success", false);
            result.put("message", e.getMessage());
            
            return ResponseEntity.badRequest().body(result);
        }
    }

    /**
     * 手动触发用户数据统计（任务调度示例）
     */
    @PostMapping("/statistics")
    public ResponseEntity<Map<String, Object>> triggerStatistics() {
        log.info("手动触发用户数据统计");
        
        try {
            userInfoService.statisticsUserData();
            
            Map<String, Object> result = new HashMap<>();
            result.put("success", true);
            result.put("message", "数据统计已触发");
            
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            log.error("数据统计触发失败", e);
            
            Map<String, Object> result = new HashMap<>();
            result.put("success", false);
            result.put("message", e.getMessage());
            
            return ResponseEntity.badRequest().body(result);
        }
    }
}